package com.runing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.runing.config.ResponseWrapper;
import com.runing.entity.Message;
import com.runing.mapper.MessageMapper;
import com.runing.service.MessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lel
 * @since 2021-03-13
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {
    @Autowired
    MessageMapper messageMapper;

    public ResponseWrapper addMsg(Message message){
        if (message == null){
            return ResponseWrapper.markCustom(false,400,"message为空",null);
        }else {
            int resilt = messageMapper.insert(message);
            if (resilt == 1){
                return ResponseWrapper.markSuccess(null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }

    public ResponseWrapper findMsg(Integer mid){
        List<Message> messages = new ArrayList<>();
        if (mid == null){
            messages = messageMapper.selectList(null);
        }else {
            Message message = messageMapper.selectById(mid);
            messages.add(message);
        }
        ResponseWrapper wrapper = ResponseWrapper.markSuccess(messages);
        wrapper.getData().put("date",messages);
        return wrapper;
    }

}
